/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.analyzer.cluster;

import com.aliasi.cluster.Clusterer;
import com.aliasi.cluster.CompleteLinkClusterer;
import com.aliasi.cluster.SingleLinkClusterer;
import com.aliasi.util.Distance;
import java.util.List;
import java.util.Map;
import java.util.Set;
import pl.tom.social.analyzer.common.MultiClusterType;
import pl.tom.social.analyzer.common.Results;
import pl.tom.social.analyzer.visualizer.AnalysisVisualizer;
import pl.tom.social.common.types.AnalysisType;

/**
 *
 * @author Tom
 */
public class LingpipeCluster {

	public static void cluster(Map<String, List<Double>> scores, MultiClusterType clusterType, double maxDistance) {
		Distance<String> distance = new SocialDistance(scores);
		Clusterer<String> cluster = null;
		switch (clusterType) {
			case SingleLink:
				cluster = new SingleLinkClusterer<String>(maxDistance, distance);
				break;
			case CompleteLink:
				cluster = new CompleteLinkClusterer<String>(maxDistance, distance);
				break;
			default:
				return;
		}
		Set<Set<String>> result = cluster.cluster(scores.keySet());
		Results.show(AnalysisVisualizer.setsToJTable(result), AnalysisType.Clustering);
	}

	private static class SocialDistance implements Distance<String> {

		private Map<String, List<Double>> results;

		public SocialDistance(Map<String, List<Double>> results) {
			this.results = results;
		}

		public double distance(String s1, String s2) {
			List<Double> i1 = results.get(s1);
			List<Double> i2 = results.get(s2);
			double avg1 = 0;
			double avg2 = 0;
			for (double d : i1) {
				avg1 += d;
			}
			for (double d : i2) {
				avg2 += d;
			}
			avg1 /= i1.size();
			avg2 /= i2.size();
			return Math.abs(avg1 - avg2);
		}
	}
}


